package org.iteye.bbjava.util.abstractfactory.concre;

import org.iteye.bbjava.util.abstractfactory.AbstractFactory;
import org.iteye.bbjava.util.abstractfactory.ImplementClassProduct;
import org.iteye.bbjava.util.abstractfactory.InterfaceProduct;
import org.iteye.bbjava.util.interpreter.Context;
import org.iteye.bbjava.util.interpreter.NonTerminalExpression;
import org.iteye.bbjava.util.interpreter.TerminalExpression;

public class JavaDaoFileCodeFactory implements AbstractFactory {

	private String entityXml;
	
	
	
	public JavaDaoFileCodeFactory(String entityXml) {
		super();
		this.entityXml = entityXml;
	}
	
	
	public JavaDaoFileCodeFactory() {
		super();
	}
	

	@Override
	public InterfaceProduct createInterface() {
		Context ctx = new Context();
		InterfaceProduct interfaceProduct = null;
		if(null !=this.entityXml && !"".equals(entityXml)){
			interfaceProduct = new  InterfaceProudctImpl();
			ctx.setXml(entityXml);
			NonTerminalExpression nte = new NonTerminalExpression();
			TerminalExpression te = new TerminalExpression();			
			nte.setContext(ctx);
			te.setContext(ctx);
			String[] interfaceName = nte.interpret();
			String propertiesAndMethods = nte.createFinalResult("createImplementClass");
			String codeStr = this.createJavaIDaoFileCode(interfaceName[0], propertiesAndMethods, "");
			interfaceProduct.setCodeStr(codeStr);
		}
		return interfaceProduct;
	}

	@Override
	public ImplementClassProduct createImplementClass() {
		Context ctx = new Context();
		ImplementClassProduct implementClassProduct = null;
		if(null !=this.entityXml && !"".equals(entityXml)){
			implementClassProduct = new  ImplementClassProductImpl();
			ctx.setXml(entityXml);
			NonTerminalExpression nte = new NonTerminalExpression();
			TerminalExpression te = new TerminalExpression();			
			nte.setContext(ctx);
			te.setContext(ctx);
			String[] classinterfaceName = nte.interpret();
			String propertiesAndMethods = nte.createFinalResult("createImplementClass");
			String codeStr = this.createJavaDaoImplFileCode(classinterfaceName[0], propertiesAndMethods, "");
			implementClassProduct.setCodeStr(codeStr);
		}
		return implementClassProduct;
	}

	public void setEntityXml(String entityXml) {
		this.entityXml = entityXml;
	}

	/**
	 * 产生java实体代码,包含setter,getter
	 * @param interfaceName 接口名
	 * @param propertiesAndMethods 属性和方法
	 * @param packageName 包名
	 * 
	 * */
	private String createJavaIDaoFileCode(String interfaceName,String propertiesAndMethods,String packageName){
		StringBuffer sb = new StringBuffer();
		if(null!= interfaceName && !"".equals(interfaceName)){
			
			if(null != packageName ||!"".equals(packageName)){
				sb.append("package "+packageName+";");
			}
			sb.append("public class I"+interfaceName+"Dao { ");
			sb.append(propertiesAndMethods);
			sb.append(" } ");
		}		
		return sb.toString();
	}
	
	
	/**
	 * 产生java实体代码,包含setter,getter
	 * @param className 类名
	 * @param propertiesAndMethods 属性和方法
	 * @param packageName 包名
	 * 
	 * */
	private String createJavaDaoImplFileCode(String className,String propertiesAndMethods,String packageName){
		StringBuffer sb = new StringBuffer();
		if(null!= className && !"".equals(className) && null!= propertiesAndMethods && !"".equals(propertiesAndMethods)){
			
			if(null != packageName ||!"".equals(packageName)){
				sb.append("package "+packageName+";");
			}
			sb.append("public class "+className+"DaoImpl implements I"+className+"Dao { ");
			sb.append(propertiesAndMethods);
			sb.append(" } ");
		}		
		return sb.toString();
	}
	 
}
